package com.ruoyi.hotel.controller;

import java.util.List;
import java.util.Arrays;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.hotel.domain.HmPurchaseApply;
import com.ruoyi.hotel.domain.HmSupply;
import com.ruoyi.hotel.domain.bo.HmPurchaseApplyBo;
import com.ruoyi.hotel.domain.bo.HmSupplyPurchaseBo;
import com.ruoyi.hotel.domain.vo.HmPurchaseApplyVo;
import com.ruoyi.hotel.mapper.HmPurchaseApplyMapper;
import com.ruoyi.hotel.mapper.HmSupplyMapper;
import com.ruoyi.hotel.service.IHmSupplyPurchaseService;
import com.ruoyi.hotel.service.IHmSupplyService;
import com.ruoyi.system.service.ISysUserService;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.hotel.service.IHmPurchaseApplyService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 采购申请
 *
 * @date 2023-10-30
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/hotel/purchaseApply")
public class HmPurchaseApplyController extends BaseController {

    private final IHmPurchaseApplyService iHmPurchaseApplyService;
    private final IHmSupplyService iHmSupplyService;

    /**
     * 查询采购申请列表
     */
    @SaCheckPermission("hotel:purchaseApply:list")
    @GetMapping("/list")
    public TableDataInfo<HmPurchaseApplyVo> list(HmPurchaseApplyBo bo, PageQuery pageQuery) {
        return iHmPurchaseApplyService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出采购申请列表
     */
    @SaCheckPermission("hotel:purchaseApply:export")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HmPurchaseApplyBo bo, HttpServletResponse response) {
        List<HmPurchaseApplyVo> list = iHmPurchaseApplyService.queryList(bo);
        ExcelUtil.exportExcel(list, "【请填写功能名称】", HmPurchaseApplyVo.class, response);
    }

    /**
     * 获取采购申请详细信息
     *
     * @param purchaseApplyId 主键
     */
    @SaCheckPermission("hotel:purchaseApply:query")
    @GetMapping("/{purchaseApplyId}")
    public R<HmPurchaseApplyVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long purchaseApplyId) {
        return R.ok(iHmPurchaseApplyService.queryById(purchaseApplyId));
    }

    /**
     * 新增采购申请
     */
    @SaCheckPermission("hotel:purchaseApply:add")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody HmPurchaseApplyBo bo) {
        Long id=getUserId();
        bo.setWorkerId(id);
        bo.setIsPass("w");

        return toAjax(iHmPurchaseApplyService.insertByBo(bo));
    }

    /**
     * 修改采购申请
     */
    @SaCheckPermission("hotel:purchaseApply:edit")
    @Log(title = "【请填写功能名称】", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody HmPurchaseApplyBo bo) {
        return toAjax(iHmPurchaseApplyService.updateByBo(bo));
    }

    /**
     * 获取物资
     * @return TableDataInfo<HmSupply>
     */
    @GetMapping("/getSupply")
    public TableDataInfo<HmSupply> getSupply() {
        return TableDataInfo.build(iHmSupplyService.selectList(new QueryWrapper<>()));
    }

    /**
     * 通过申请
     * @param hmPurchaseApply
     */
    @PostMapping("/approve")
    public void applyPass(@RequestBody HmPurchaseApplyBo hmPurchaseApply){
        hmPurchaseApply.setIsPass("y");
        iHmPurchaseApplyService.updateByBo(hmPurchaseApply);
    }


    /**
     * 申请拒绝
     * @param hmPurchaseApply
     */
    @PostMapping("/reject")
    public void reject(@RequestBody HmPurchaseApplyBo hmPurchaseApply){
        hmPurchaseApply.setIsPass("n");
        iHmPurchaseApplyService.updateByBo(hmPurchaseApply);
    }

}
